package cloud.xuxiaowei.core.filter;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.util.ContentCachingResponseWrapper;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 响应流过滤器：记录响应内容
 *
 * @author xuxiaowei
 * @since 0.0.1
 */
@Slf4j
@Setter
@Component
public class HttpServletResponseOutputStreamFilter extends HttpFilter implements Ordered {

	public static final int ORDERED = Ordered.HIGHEST_PRECEDENCE + 100;

	private int order = ORDERED;

	@Override
	public int getOrder() {
		return this.order;
	}

	@Override
	protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		// 创建一个响应缓存
		ContentCachingResponseWrapper contentCachingResponseWrapper = new ContentCachingResponseWrapper(response);

		chain.doFilter(request, contentCachingResponseWrapper);

		// 需要在 FilterChain.doFilter 之后，才能获取到响应类型
		String contentType = response.getContentType();
		// 仅记录 JSON 响应
		if (MediaType.APPLICATION_JSON_VALUE.equals(contentType)) {

			// 获取响应流
			byte[] contentAsByteArray = contentCachingResponseWrapper.getContentAsByteArray();

			String responseBody = new String(contentAsByteArray);

			log.info("HttpServletResponse Body: {}", responseBody);
		}

		// 将响应流复制到响应流中
		// 流只能获取一次，如果此处不复制，则后面无法获取响应
		contentCachingResponseWrapper.copyBodyToResponse();

	}

}
